package com.crime.crimemonitorclient;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Random;
import java.util.TreeSet;

import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.SimpleXYSeries.ArrayFormat;

public class MonthSeriesManager extends TimeSeriesManager
{
	@Override
	protected void initTimeGranularity() {
		this.mTimeGranularity = TimeGranularity.Month;
	}

	@Override
	protected void initTimeSeriesFormatter() {
		this.mTimeSeriesFormatter = new LineAndPointFormatter();
	}

	@Override
	protected String getTimeMarkerFormattingString() {
		return "MMMM";
	}

	@Override
	protected String getTimeMarkerEndPointFormattingString() {
		return "yyyy";
	}

	@Override
	public long getMinimumDisplayTimeRange() {
		return 1 * 2592000000L;
	}

	@Override
	protected void loadObservations()
	{
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		
		start.setTimeInMillis(this.mMinTime);
		end.setTimeInMillis(this.mMaxTime);
		
		start.clear(Calendar.DAY_OF_YEAR);
		start.clear(Calendar.HOUR);
		start.clear(Calendar.MINUTE);
		start.clear(Calendar.SECOND);
		start.clear(Calendar.MILLISECOND);
		
		final int MAX_CRIMES = 100;
		Random obsValueRand = new Random();
		
		while(start.before(end))
		{
			long obsTime = start.getTimeInMillis();
			int obsValue = obsValueRand.nextInt(MAX_CRIMES);
			
			this.mObservations.add(
					new CrimeTimeSeriesObservation(obsTime, obsValue));
			
			start.add(Calendar.MONTH, 1);
		}
		
		SimpleXYSeries thisSeries = (SimpleXYSeries)this.mTimeSeries;
		/// TODO: hack in order to clear all of the series' values
		//thisSeries.setModel(new ArrayList<Number>(), ArrayFormat.Y_VALS_ONLY);
		while(thisSeries.size() > 0)
		{
			thisSeries.removeLast();
		}
		
		for(CrimeTimeSeriesObservation observation : this.mObservations)
		{
			thisSeries.addLast(observation.getObsTime(), observation.getObsValue());
		}
	}

	@Override
	public long getMaximumDisplayTimeRange() {
		return 12 * 2592000000L;
	}

}
